﻿using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using BLL;
using Model;

public partial class ProductManage_RFProductManage_Manage_ProductType_ZTEManage : System.Web.UI.Page
{
    private ProductType_ZTE productType_ZTE;

    public ProductManage_RFProductManage_Manage_ProductType_ZTEManage()
    {
        productType_ZTE = new ProductType_ZTE();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindProductType_ZTE();
        }
    }

    protected void BindProductType_ZTE()
    {
        GridView1.DataSource = productType_ZTE.LoadEntities();
        GridView1.DataBind();
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindProductType_ZTE();
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindProductType_ZTE();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[8].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string productTypeID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
        productType_ZTE.Delete(productTypeID);
        BindProductType_ZTE();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindProductType_ZTE();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        ProductType_ZTEInfo productTypeInfo = new ProductType_ZTEInfo();
        productTypeInfo.ProductTypeID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();        
        productTypeInfo.Itemcode = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.Trim().ToString();
        productTypeInfo.ModuleType = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim().ToString();
        productTypeInfo.Vender = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.Trim().ToString();
        productTypeInfo.Remark1 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.Trim().ToString();
        productTypeInfo.Remark2 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[5].Controls[0])).Text.Trim().ToString();
        productTypeInfo.Remark3 = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[6].Controls[0])).Text.Trim().ToString();

        productType_ZTE.Update(productTypeInfo);
        GridView1.EditIndex = -1;
        BindProductType_ZTE();
    }

    protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        DetailsView d = (DetailsView)this.GridView1.Controls[0].Controls[0].FindControl("DetailsView1");
        ProductType_ZTEInfo productTypeInfo = new ProductType_ZTEInfo();
        productTypeInfo.ProductTypeID = ((TextBox)(d.Rows[0].Cells[1].Controls[0])).Text.Trim();
        productTypeInfo.Itemcode = ((TextBox)(d.Rows[1].Cells[1].Controls[0])).Text.Trim();
        productTypeInfo.ModuleType = ((TextBox)(d.Rows[2].Cells[1].Controls[0])).Text.Trim();
        productTypeInfo.Vender = ((TextBox)(d.Rows[3].Cells[1].Controls[0])).Text.Trim();
        productTypeInfo.Remark1 = ((TextBox)(d.Rows[4].Cells[1].Controls[0])).Text.Trim();
        productTypeInfo.Remark2 = ((TextBox)(d.Rows[5].Cells[1].Controls[0])).Text.Trim();
        productTypeInfo.Remark3 = ((TextBox)(d.Rows[6].Cells[1].Controls[0])).Text.Trim();

        ProductType_ZTEInfo pro = productType_ZTE.LoadEntity(productTypeInfo.ProductTypeID);
        ProductType p = new ProductType();
        ProductTypeInfo pi = p.LoadEntity(productTypeInfo.ProductTypeID);
        if (pro.ProductTypeID == productTypeInfo.ProductTypeID)
        {
            string str1 = "此产品类型信息已存在中兴产品项目信息表中！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            if (string.IsNullOrEmpty(pi.ProductTypeID))
            {
                string str1 = "此产品类型不是正常的在制产品类型，请确认！";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
            }
            else
            {
                productType_ZTE.Insert(productTypeInfo);
                string str1 = "添加成功！";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
                BindProductType_ZTE();
            }
        }
    }

    protected void DetailsView1_ModeChanging(object sender, DetailsViewModeEventArgs e)
    {
        BindProductType_ZTE();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Insert")
        {
            GridView1.DataSource = null;
            GridView1.DataBind();
        }
    }
}
